const fs = require("fs");
const path = require("path");

module.exports = app => {
  const apiPath = path.resolve(__dirname, "./api");
  findSync(apiPath).forEach(dir => require(dir)(app));
};

function findSync(filePath) {
  const result = [];
  const files = fs.readdirSync(filePath);
  files.forEach(val => {
    const file = path.join(filePath, val);
    const stats = fs.statSync(file);
    if (stats.isDirectory()) {
      result.push(...findSync(file));
    } else if (stats.isFile()) {
      result.push(file);
    }
  });
  return result;
}
